package cz.byteworks.android.myway.activities;

import java.text.SimpleDateFormat;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import cz.byteworks.android.myway.adapter.PositionArrayAdapter;
import cz.byteworks.android.myway.db.DataHelper;
import cz.byteworks.android.myway.pojo.Position;
import cz.byteworks.android.myway.pojo.Way;

public class MyWayDetailActivity extends Activity {

	List<Position> posList;
	ArrayAdapter<Position> dataAdapter;
	ListView lv;
	Way way;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		way = (Way) getIntent().getExtras().getSerializable("wayObj");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.position_list);
		lv = (ListView) findViewById(R.id.positionListView);
		DataHelper dh = new DataHelper(this);
		posList = dh.getAllPositions(way.getId(), true);
		dataAdapter = new PositionArrayAdapter<Position>(this, R.layout.position_item, posList, getLayoutInflater());
		lv.setAdapter(dataAdapter);
		lv.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3) {
				view.showContextMenu();
			}
		});
		registerForContextMenu(lv);
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.pos_context_menu, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		Position pos = posList.get(info.position);
		if (item.getTitle().toString().equals(getResources().getString(R.string.pos_context_menu_open))) {
			Intent intent = new Intent(this, MyWayMapActivity.class);
			intent.putExtra("wayObj", way);
			intent.putExtra("markTo", pos);
			startActivity(intent);
		}
		if (item.getTitle().toString().equals(getResources().getString(R.string.pos_context_menu_note))) {
			menuPopupAddNote(pos.getPid(), pos.getNote());
		}
		if (item.getTitle().toString().equals(getResources().getString(R.string.pos_context_menu_delete))) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm");
			menuPopupDeletePosition(pos.getPid(), sdf.format(pos.getLocateTime()));
		}
		return true;
	}

	private void menuPopupAddNote(final int pid, String actualNote) {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle("Edit note");
		final EditText input = new EditText(this);
		input.setText(actualNote);
		alert.setView(input);
		final MyWayDetailActivity ctx = this;
		alert.setPositiveButton("Save", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				DataHelper dh = new DataHelper(ctx);
				dh.updatePosNote(pid, input.getText().toString());
				Toast.makeText(ctx, "Note saved", 4000).show();
				dataAdapter.notifyDataSetChanged();
				onCreate(null);
			}
		});
		alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				// Canceled.
			}
		});

		alert.show();
	}

	private void menuPopupDeletePosition(final int pid, String date) {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle("Delete Position");
		alert.setMessage("Do you really want to delete Position from date '"+date+"'?");

		final MyWayDetailActivity ctx = this;
		alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				DataHelper dh = new DataHelper(ctx);
				dh.deletePosition(pid);
				Toast.makeText(ctx, "Position deleted", 4000).show();
				dataAdapter.notifyDataSetChanged();
				onCreate(null);
			}
		});
		alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				// Canceled.
			}
		});

		alert.show();
	}
}
